import { isUrl } from '../utils/utils';

const menuData = [
  {
    name: '个人中心',
    icon: 'user',
    path: 'center',
    children: [
      {
        name: '学生信息',
        path: 'studentinfor',
      },
      {
        name: '教师信息',
        path: 'teacherinfor',
      },
      {
        name: '修改密码',
        path: 'password',
      },
    ],
  },
  {
    name: '用户管理',
    icon: 'usergroup-add',
    path: 'usermanage',
    children: [
      {
        name: '学生档案',
        path: 'studentlist',
      },
      {
        name: '教师档案',
        path: 'teacherlist',
      },
    ],
  },
  {
    name: '上传成绩',
    icon: 'cloud-upload-o',
    path: 'confirmupload',
  },
  {
    name: '上传成绩(详情页)',
    icon: 'cloud-upload-o',
    path: 'uploadgrade',
    hideInMenu: true,
  },
  {
    name: '成绩管理',
    icon: 'table',
    path: 'course',
  },
  {
    name: '授课安排',
    icon: 'share-alt',
    path: 'teachschedule',
  },
  {
    name: '班级管理',
    icon: 'tags-o',
    path: 'classmanage',
  },
  {
    name: '专业管理',
    icon: 'exception',
    path: 'professionmanage',
  },
  {
    name: '课程管理',
    icon: 'inbox',
    path: 'coursemanage',
  },
];

function formatter(data, parentPath = '/', parentAuthority) {
  return data.map(item => {
    let { path } = item;
    if (!isUrl(path)) {
      path = parentPath + item.path;
    }
    const result = {
      ...item,
      path,
      authority: item.authority || parentAuthority,
    };
    if (item.children) {
      result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority);
    }
    return result;
  });
}

export const getMenuData = () => {
  let menu = JSON.parse(localStorage.getItem('menu'));
  if (menu === undefined || menu === null) {
    menu = menuData;
  }
  return formatter(menu);
};
